﻿#include "TableChartWindowSidebarData.h"
#include <QSet>
#include "BaseModule/GlobalHelper.h"

namespace pst
{
    TableChartWindowSidebarData::TableChartWindowSidebarData(QObject* parent) : QObject(parent)
    {
    }

    TableChartWindowSidebarData::~TableChartWindowSidebarData()
    {
    }

    void TableChartWindowSidebarData::appendTableDataitemInformation(TableDataitemInformation dataitemInformation)
    {
        for (auto& _dataitemInformation : m_tableDataitemInformationList)
        {
            if (_dataitemInformation.m_itemRowIndex == dataitemInformation.m_itemRowIndex)
            {
                _dataitemInformation.m_itemName = dataitemInformation.m_itemName;
                _dataitemInformation.m_itemDisplayStatus = dataitemInformation.m_itemDisplayStatus;
                _dataitemInformation.m_isReNamed = dataitemInformation.m_isReNamed;
                _dataitemInformation.m_isEndWithKey = dataitemInformation.m_isEndWithKey;
                return;
            }
        }
        m_tableDataitemInformationList.append(dataitemInformation);
    }

    QList<TableChartWindowSidebarData::TableDataitemInformation>& TableChartWindowSidebarData::getTableDataitemInformationList()
    {
        return m_tableDataitemInformationList;
    }

    void TableChartWindowSidebarData::changeTableDataItemDisplayStatus(QSet<int> RowList)
    {
        for (auto _row : RowList)
        {
            for (auto _dataitemInformation : m_tableDataitemInformationList)
            {
                if (_row == _dataitemInformation.m_itemRowIndex)
                {
                    _dataitemInformation.m_itemDisplayStatus = !_dataitemInformation.m_itemDisplayStatus;
                }
            }
        }
    }

    void TableChartWindowSidebarData::changeTableDataItemName(QHash<int, QString> itemIndexAndName)
    {
        auto _indexs = itemIndexAndName.keys();
        for (auto _row : _indexs)
        {
            for (auto _dataitemInformation : m_tableDataitemInformationList)
            {
                if (_row == _dataitemInformation.m_itemRowIndex)
                {
                    _dataitemInformation.m_itemName = itemIndexAndName[_row];
                }
            }
        }
    }

    void TableChartWindowSidebarData::appendDataList(QHash<int, QStringList> hash)
    {
        for (auto _key : m_dataHash.keys())
        {
            if (_key == hash.keys().first())
            {
                m_dataHash.remove(_key);
                m_dataHash.insert(_key, hash[_key]);
                return;
            }
        }
        m_dataHash.insert(hash.keys().first(), hash[hash.keys().first()]);
    }
    QHash<int, QStringList> TableChartWindowSidebarData::getDataList()
    {
        return m_dataHash;
    }

    void TableChartWindowSidebarData::clear()
    {
        m_tableDataitemInformationList.clear();
        m_dataHash.clear();
    }

    /// @brief 文件写出至工程文件
    QDomElement& TableChartWindowSidebarData::writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso)
    {
        QDomElement _TableChartWindowSidebarDataRoot = doc->createElement("TableChartWindowSidebarData");

        auto _tableDataitemInformationListStr = tableDataitemInformationListToString(m_tableDataitemInformationList);
        GlobalHelperInstance->createDomElement(doc, &_TableChartWindowSidebarDataRoot, "TableDataitemInformationList", _tableDataitemInformationListStr);
        auto _dataListStr = GlobalHelperInstance->convertQHashIntQStringListToQString(m_dataHash);
        GlobalHelperInstance->createDomElement(doc, &_TableChartWindowSidebarDataRoot, "DataHash", _dataListStr);

        QList<TableDataitemInformation> m_tableDataitemInformationList{};

        element->appendChild(_TableChartWindowSidebarDataRoot);

        return _TableChartWindowSidebarDataRoot;
    }
    /// @brief 从工程文件读入数据
    void TableChartWindowSidebarData::readFromProjectFile(QDomElement* element, bool isdiso)
    {
        auto _TableDataitemInformationListNodeList = element->elementsByTagName("TableDataitemInformationList");
        auto _tableDataitemInformationListStr = _TableDataitemInformationListNodeList.item(0).toElement().text();
        m_tableDataitemInformationList = stringToTableDataitemInformationList(_tableDataitemInformationListStr);
        auto _DataListNodeList = element->elementsByTagName("DataHash");
        auto _dataListStr = _DataListNodeList.item(0).toElement().text();
        m_dataHash = GlobalHelperInstance->convertQStringToQHashIntQStringList(_dataListStr);
    }

    QString TableChartWindowSidebarData::tableDataitemInformationListToString(QList<TableChartWindowSidebarData::TableDataitemInformation> dataitemInformationList)
    {
        QString _tableDataitemInformationListString;
        for (auto _dataitemInformation : dataitemInformationList)
        {
            _tableDataitemInformationListString += 
                QString::number(_dataitemInformation.m_itemRowIndex) + "," + 
                _dataitemInformation.m_itemName + "," + 
                QString::number(_dataitemInformation.m_itemDisplayStatus) + "," + 
                QString::number(_dataitemInformation.m_isReNamed) + "," + 
                QString::number(_dataitemInformation.m_isEndWithKey) + ";";
        }
        return _tableDataitemInformationListString;

    }
    QList<TableChartWindowSidebarData::TableDataitemInformation> TableChartWindowSidebarData::stringToTableDataitemInformationList(QString dataitemInformationListString)
    {
        QList<TableChartWindowSidebarData::TableDataitemInformation> _tableDataitemInformationList;
        QStringList _dataitemInformationListStringList = dataitemInformationListString.split(";");
        for (auto _dataitemInformationString : _dataitemInformationListStringList)
        {
            QStringList _dataitemInformationStringList = _dataitemInformationString.split(",");
            if (_dataitemInformationStringList.count() == 5)
            {
                TableChartWindowSidebarData::TableDataitemInformation _dataitemInformation;
                _dataitemInformation.m_itemRowIndex = _dataitemInformationStringList.at(0).toInt();
                _dataitemInformation.m_itemName = _dataitemInformationStringList.at(1);
                _dataitemInformation.m_itemDisplayStatus = (bool)_dataitemInformationStringList.at(2).toInt();
                _dataitemInformation.m_isReNamed = (bool)_dataitemInformationStringList.at(3).toInt();
                _dataitemInformation.m_isEndWithKey = (bool)_dataitemInformationStringList.at(4).toInt();
                _tableDataitemInformationList.append(_dataitemInformation);
            }
        }
        return _tableDataitemInformationList;
    }

} // namespace pst
